<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\logic;

use easemob\EasemobIm;

class EasemoblmLogic
{
    private $_owner; //管理员


    public function __construct()
    {
        $platform = sysConfig('platform');
        $this->_owner = isset($platform['customer_service_member_id']) ? $platform['customer_service_member_id'] : 1;
    }

    public function joinSysGroup($mid, $groupname = null, $desc = null, $maxusers = 2000)
    {
        if ($maxusers > 2000) $maxusers = 2000;
        $id = floor($mid / $maxusers) + 1;  // 0-1999  2000-3999
        $easemoblmGroupModel = model('common/easemoblm/EasemoblmGroup');
        $group = $easemoblmGroupModel->getInfoByID($id);
        $easemoblm = new EasemobIm();
        if (empty($group)) { //
            if (is_null($groupname)) $groupname = '系统群' . $id;
            if (is_null($desc)) $desc = '用于群发系统消息';

            $res = $easemoblm->createGroup($groupname, $desc, $this->_owner, [], true, $maxusers);
            if ($res['code'] != SUCCESS) return ds_arr($res['code'], $res['msg'], $res['result']);
            $groupid = $res['result']['data']['groupid'];

            $add_chat_room = $easemoblmGroupModel->add([
                'id' => $id,
                'groupid' => $groupid,
                'groupname' => $groupname,
                'desc' => $desc,
                'owner' => $this->_owner
            ]);

        } else {
            $groupid = $group['groupid'];
        }
        if ($mid == $this->_owner) return ds_arr(SUCCESS, '加入成功', ['data' => ['groupid' => $groupid]]);

        //加入聊天室
        $res = $easemoblm->addGroupMember($groupid, $mid);
        return ds_arr($res['code'], $res['msg'], $res['result']);
    }


}
